package com.ulife.permission.module.screen;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.ulife.common.ErrorMessage;
import com.ulife.common.exception.ManagerException;
import com.ulife.permission.common.DepartmentQueryResult;
import com.ulife.permission.dao.dataobject.DepartmentDO;
import com.ulife.permission.manager.DepartmentManager;

public class DepartmentList {

	private static final Logger log = LoggerFactory.getLogger(DepartmentList.class);

	@Autowired
	private DepartmentManager departmentManager;
	
	public void execute(Context context, Navigator nav) {
		try {
			context.put("module", "department");
			DepartmentQueryResult result = departmentManager.getAllDepartment();
			List<DepartmentDO> departmentList = null;
			if (result == null || !result.isSuccess()) {
				context.put("error", ErrorMessage.ROLE_INFO_ERROR);
				nav.forwardTo("error");
			}
			if (CollectionUtils.isNotEmpty(result.getDepartmentList())){
				departmentList = result.getDepartmentList();
			}
			context.put("departments", departmentList);
		} catch (ManagerException e) {
			context.put("error", ErrorMessage.ROLE_INFO_ERROR);
			nav.forwardTo("error");
			log.error("get user list error",e);
		}
	}
}
